package com.breaktian.demoapp.hotfix.andfix;

import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;

import com.alipay.euler.andfix.patch.PatchManager;
import com.breaktian.assemble.utils.AppInfoUtil;
import com.breaktian.assemble.utils.FileUtil;
import com.breaktian.assemble.utils.LogUtil;
import com.breaktian.assemble.utils.ThreadUtil;
import com.breaktian.network.download.DownLoadListener;
import com.breaktian.network.download.HttpDownload;

import java.io.File;

/**
 * Created by keke.tian on 2018/5/10.
 *
 * AndFix加载策略
 *
 */

public class AndFixManager {


    private PatchManager mPatchManager;

    private String patchUrl = "http://www.hbounty.com/test/andfix/v_%s/dest.apatch";

    private String patchPath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/andfix_ptch/";
    private String patchName = "out.apatch";

    static class InstanceHolder{
        final static AndFixManager instance = new AndFixManager();
    }

    public static AndFixManager getInstance(){
        return InstanceHolder.instance;
    }

    private AndFixManager(){
    }

    public void init(Context context){
        mPatchManager = new PatchManager(context);
        //每次appversion变更都会导致所有补丁被删除,如果appversion没有改变，则会加载已经保存的所有补丁。
        mPatchManager.init(String.valueOf(AppInfoUtil.getVersionCode(context)));
        mPatchManager.loadPatch();
//        //根据版本号加载patch
//        loadPatchFromServer(String.format(patchUrl,AppInfoUtil.getVersionName(context)));
    }

    public void loadPatchFromServer(String url){
        File patchFile = FileUtil.createFile(patchPath,patchName);
        if(patchFile==null){
            LogUtil.i("load patch from server fail patchFile create fail ["+patchPath+patchName+"]");
            return;
        }
        LogUtil.i("load patch from server patchFile create success ["+patchPath+patchName+"]");
        LogUtil.i("load patch from server url : "+url);
        HttpDownload.get().downloadFile(url, patchFile, new DownLoadListener() {
            @Override
            public void onFail(File file) {
                LogUtil.i("load patch from server fail");
            }

            @Override
            public void onSucess(final File file) {
                LogUtil.i("load patch from server onSucess:"+file==null?"null":file.getAbsolutePath());
                ThreadUtil.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        addPath(file);
                        //修复完之后删除patch文件
                        FileUtil.deleteFile(file);
                    }
                });
            }

            @Override
            public void onProgress(long bytesRead, long contentLength, boolean done) {
                LogUtil.i("load patch from server onProgress "+bytesRead+"/"+contentLength+" done:"+done);
            }
        });
    }

    private void addPath(File file) {
        try {
            if(file!=null && !TextUtils.isEmpty(file.getAbsolutePath())){
                mPatchManager.addPatch(file.getAbsolutePath());
                LogUtil.i("addPath patch success file:"+file.getAbsolutePath());
            }else{
                LogUtil.i("addPath patch fail file:"+file==null?"null":file.getAbsolutePath());
            }
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.i("addPath patch Exception :"+e.getMessage());
        }
    }


}
